<template>
  <div class="app-container">
    <!--文号字号查询-->
    <el-row :gutter="20">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="查询内容" prop="queryInfo">
            <el-input v-model="queryParams.queryInfo" placeholder="请输入流程Key或字号查询" clearable size="small"
              style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
    </el-row>
 <hr style="width:100%;border:1px solid grey;">
    <!--按钮-->
    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" plain size="mini" @click="add()"
          v-hasPermi="['system:oa:FileWord:edit']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:oa:FileWord:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:oa:FileWord:remove']">删除</el-button>
      </el-col>
    </el-row>
    <!--文件字号配置数据列表-->
    <el-table v-loading="loading" :data="FileWordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="流程Key" align="center" prop="fileType" :show-overflow-tooltip="true" />
      <el-table-column label="字号" align="center" prop="wordName" />
      <el-table-column label="归属组织" align="center" prop="deptName" :show-overflow-tooltip="true" />
      <el-table-column label="稿纸题头" align="center" prop="draftTitle" :show-overflow-tooltip="true" />
      <el-table-column label="公文红头" align="center" prop="redHead" :show-overflow-tooltip="true" />

      <el-table-column label="右下落款" align="center" prop="rightBottom" :show-overflow-tooltip="true" />
      <el-table-column label="左下落款" align="center" prop="leftBottom" :show-overflow-tooltip="true" />
      <el-table-column label="修改时间" align="center" prop="modifiTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifiTime.toLocaleString().replace(/T/g, ' ').substring(0,19) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="userCN" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!--数据编辑输入对话框-->
    <el-dialog :title="FileWordDialogTitle" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="记录ID" prop="id" style="display:none;">
          <el-input v-model="form.id" placeholder="请输入ID" />
        </el-form-item>
        <el-form-item label="流程Key" prop="fileType" style="font-style: normal;">
          <el-input v-model="form.fileType" placeholder="请输入或选择流程Key" style="width:80%;padding-right:10px;" />
          <el-button type="primary" icon="el-icon-search" @click="selectFlow">选择流程</el-button>
        </el-form-item>
        <el-form-item label="文件字号" prop="wordName">
          <el-input v-model="form.wordName" placeholder="请输入文件字号" style="width:30%" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请选择部门或公司" style="width:80%;padding-right:10px;" />
          <el-button type="primary" icon="el-icon-search" @click="selectDept">选择部门</el-button>
        </el-form-item>
        <el-form-item label="稿纸题头" prop="draftTitle">
          <el-input v-model="form.draftTitle" placeholder="请输入公文稿纸题头" />
        </el-form-item>
        <el-form-item label="公文正式红头" prop="redHead">
          <el-input v-model="form.redHead" placeholder="请输入公文正式红头" />
        </el-form-item>
        <el-form-item label="文件右下落款" prop="rightBottom">
          <el-input v-model="form.rightBottom" placeholder="请输入文件右下落款名称" />
        </el-form-item>
        <el-form-item label="文件左下落款" prop="leftBottom">
          <el-input v-model="form.leftBottom" placeholder="请输入文件左下落款名称" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model='form.status'>
            <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addFileWord,
    listFileWord,
    updateFileWord,
    getFileWord,
    delConfig,
    changeFileWordStatus,
    listFileWordForKey
  } from "@/api/system/oa/FileWordConfig";

  export default {
    name: "FileWord",
    data() {
      return {
        FileWordDialogTitle: "文件字号管理",
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        FileWordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 类型数据字典
        typeOptions: [],
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          queryInfo: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          fileType: [{
            required: true,
            message: "流程Key不能为空",
            trigger: "blur"
          }],
          wordName: [{
            required: true,
            message: "文件字号不能为空",
            trigger: "blur"
          }],
          rightBottom: [{
            required: true,
            message: "文件右下落款不能为空",
            trigger: "blur"
          }],
          leftBottom: [{
            required: true,
            message: "文件左下落款不能为空",
            trigger: "blur"
          }],
          draftTitle:[{
            required: true,
            message: "稿纸题头不能为空",
            trigger: "blur"
          }],
          redHead:[{
            required: true,
            message: "公文红头不能为空",
            trigger: "blur"
          }],

        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        listFileWord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.FileWordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getListForKey();
      },
      getListForKey() {
        this.loading = true;
        if(this.queryParams.queryInfo!=undefined){
          listFileWordForKey(this.queryParams).then(response => {
            this.FileWordList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }else{
            this.getList();
          }
      },

      //选择流程
      selectFlow() {

      },
      //选择组织或部门
      selectDept() {

      },

      /** 新增按钮操作 */
      add() {
        this.reset();
        this.open = true;
        this.title = "添加文件字号";
      },
      reset() {
        this.form = {
          fileType: undefined,
          wordName: undefined,
          deptName: undefined,
          rightBottom: undefined,
          leftBottom: undefined,
          status: "0"
        };
        this.resetForm("form");
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id || this.ids;
        this.$confirm('是否确认删除编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfig(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getFileWord(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改参数";
        })
      },
      /** 字号状态修改 */
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$confirm('确认要' + text + '"' + row.fileType + "（" + row.wordName + "）" + '"字号吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeFileWordStatus(row.id, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateFileWord(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFileWord(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      }

    }
  };
</script>

<style>
</style>
